/*
 * MainFrame.java
 *
 * Created on April 2, 2008, 11:33 AM
 */

package com.leeall.jatalk;

import com.leeall.google.GoogleConstants;
import com.leeall.google.auth.AppsClientLogin;
import com.leeall.jatalk.common.Constants;
import com.leeall.spring.utils.SpringContextLoader;
import com.leeall.utils.string.StringUtil;
import java.awt.Color;
import javax.swing.BorderFactory;

/**
 *
 * @author  Leeall (lixiaosong@gmail.com)
 */
public class MainFrame extends javax.swing.JFrame {
    
    /** Creates new form MainFrame */
    public MainFrame() {
        initComponents();
    }
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jDialog1 = new javax.swing.JDialog();
        loginPasswordLabel = new javax.swing.JLabel();
        loginUsernameLabel = new javax.swing.JLabel();
        passwordTextField = new javax.swing.JPasswordField();
        userNameTextField = new javax.swing.JTextField();
        loginBtn = new javax.swing.JButton();
        cancelBtn = new javax.swing.JButton();
        statusLabel = new javax.swing.JLabel();
        buddyListPanel = new javax.swing.JScrollPane();
        mainMenuBar = new javax.swing.JMenuBar();
        buddyMenu = new javax.swing.JMenu();
        jMenuItem1 = new javax.swing.JMenuItem();
        accountMenu = new javax.swing.JMenu();
        toolsMenu = new javax.swing.JMenu();
        helpMenu = new javax.swing.JMenu();

        java.util.ResourceBundle bundle = java.util.ResourceBundle.getBundle("i18n/MessagesBundle"); // NOI18N
        loginPasswordLabel.setText(bundle.getString("Password:")); // NOI18N

        loginUsernameLabel.setText(bundle.getString("Username:")); // NOI18N

        passwordTextField.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                passwordTextFieldActionPerformed(evt);
            }
        });
        passwordTextField.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusGained(java.awt.event.FocusEvent evt) {
                passwordTextFieldFocusGained(evt);
            }
        });

        userNameTextField.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                userNameTextFieldActionPerformed(evt);
            }
        });
        userNameTextField.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusGained(java.awt.event.FocusEvent evt) {
                userNameTextFieldFocusGained(evt);
            }
            public void focusLost(java.awt.event.FocusEvent evt) {
                userNameTextFieldFocusLost(evt);
            }
        });

        loginBtn.setText(bundle.getString("Login")); // NOI18N
        loginBtn.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                loginBtnActionPerformed(evt);
            }
        });

        cancelBtn.setText(bundle.getString("Cancel")); // NOI18N
        cancelBtn.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cancelBtnActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jDialog1Layout = new javax.swing.GroupLayout(jDialog1.getContentPane());
        jDialog1.getContentPane().setLayout(jDialog1Layout);
        jDialog1Layout.setHorizontalGroup(
            jDialog1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jDialog1Layout.createSequentialGroup()
                .addGap(55, 55, 55)
                .addGroup(jDialog1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(loginPasswordLabel, javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(loginUsernameLabel, javax.swing.GroupLayout.Alignment.TRAILING))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jDialog1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(userNameTextField, javax.swing.GroupLayout.PREFERRED_SIZE, 165, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(passwordTextField, javax.swing.GroupLayout.PREFERRED_SIZE, 162, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(93, Short.MAX_VALUE))
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jDialog1Layout.createSequentialGroup()
                .addContainerGap(90, Short.MAX_VALUE)
                .addComponent(loginBtn)
                .addGap(45, 45, 45)
                .addComponent(cancelBtn, javax.swing.GroupLayout.PREFERRED_SIZE, 80, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(85, 85, 85))
            .addGroup(jDialog1Layout.createSequentialGroup()
                .addGap(78, 78, 78)
                .addComponent(statusLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 157, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(145, Short.MAX_VALUE))
        );

        jDialog1Layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {cancelBtn, loginBtn});

        jDialog1Layout.setVerticalGroup(
            jDialog1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jDialog1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(statusLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 28, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jDialog1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(loginUsernameLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 32, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(userNameTextField, javax.swing.GroupLayout.PREFERRED_SIZE, 26, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jDialog1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(loginPasswordLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 32, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(passwordTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(jDialog1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(loginBtn)
                    .addComponent(cancelBtn, javax.swing.GroupLayout.PREFERRED_SIZE, 28, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(33, 33, 33))
        );

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        buddyMenu.setText("Buddy");

        jMenuItem1.setText("AddBuddy");
        buddyMenu.add(jMenuItem1);

        mainMenuBar.add(buddyMenu);

        accountMenu.setText("Account");
        mainMenuBar.add(accountMenu);

        toolsMenu.setText("Tools");
        mainMenuBar.add(toolsMenu);

        helpMenu.setText("Help");
        mainMenuBar.add(helpMenu);

        setJMenuBar(mainMenuBar);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(buddyListPanel, javax.swing.GroupLayout.PREFERRED_SIZE, 248, javax.swing.GroupLayout.PREFERRED_SIZE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(buddyListPanel, javax.swing.GroupLayout.DEFAULT_SIZE, 367, Short.MAX_VALUE)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void passwordTextFieldActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_passwordTextFieldActionPerformed
        this.login();
    }//GEN-LAST:event_passwordTextFieldActionPerformed

    private void passwordTextFieldFocusGained(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_passwordTextFieldFocusGained
        this.passwordTextField.setBorder(BorderFactory.createLineBorder(Color.LIGHT_GRAY));
    }//GEN-LAST:event_passwordTextFieldFocusGained

    private void userNameTextFieldActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_userNameTextFieldActionPerformed
        this.login();
    }//GEN-LAST:event_userNameTextFieldActionPerformed

    private void userNameTextFieldFocusGained(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_userNameTextFieldFocusGained
        this.userNameTextField.setBorder(BorderFactory.createLineBorder(Color.LIGHT_GRAY));
    }//GEN-LAST:event_userNameTextFieldFocusGained

    private void userNameTextFieldFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_userNameTextFieldFocusLost
        
    }//GEN-LAST:event_userNameTextFieldFocusLost

    private void loginBtnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_loginBtnActionPerformed
        this.login();
    }//GEN-LAST:event_loginBtnActionPerformed

    private void cancelBtnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cancelBtnActionPerformed
        // TODO add your handling code here:
        System.exit(0);
    }//GEN-LAST:event_cancelBtnActionPerformed
    
    /**
     * Loging Action
     * @return  login status
     */
    private boolean login() {
        String userName = this.userNameTextField.getText();
        char[] cs = this.passwordTextField.getPassword();
        String password = String.valueOf(cs);
        boolean status = false;
        if (StringUtil.stringIsEmpty(userName)) {
            this.statusLabel.setText(java.util.ResourceBundle.getBundle("i18n/MessagesBundle").getString("Pls_input_your_username"));
            this.userNameTextField.setBorder(BorderFactory.createLineBorder(Color.RED, 2));
            return status;
        } else {
            if (!StringUtil.validateEmail(userName)) {
                this.statusLabel.setText(java.util.ResourceBundle.getBundle("i18n/MessagesBundle").getString("Invalid_Email_Address"));
                return status;
            }
        }
        if (StringUtil.stringIsEmpty(password)) {
            this.statusLabel.setText(java.util.ResourceBundle.getBundle("i18n/MessagesBundle").getString("Pls_input_your_password"));
            this.passwordTextField.setBorder(BorderFactory.createLineBorder(Color.RED, 2));
            return status;
        }
        try {
            AppsClientLogin appsClientLogin = (AppsClientLogin) SpringContextLoader.getBean("appsClientLogin");
            status = appsClientLogin.login(
                    GoogleConstants.GOOGLE_AUTH_ACCOUNT_TYPE_GOOGLE,
                    userName, password, "cl",
                    Constants.VERSION, "", "");
            if (status) {
                this.statusLabel.setText(java.util.ResourceBundle.getBundle("i18n/MessagesBundle").getString("Login_Succeed"));
            } else {
                this.statusLabel.setText(java.util.ResourceBundle.getBundle("i18n/MessagesBundle").getString("Login_Failed"));
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return status;
    }
    
    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new MainFrame().setVisible(true);
            }
        });
    }
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JMenu accountMenu;
    private javax.swing.JScrollPane buddyListPanel;
    private javax.swing.JMenu buddyMenu;
    private javax.swing.JButton cancelBtn;
    private javax.swing.JMenu helpMenu;
    private javax.swing.JDialog jDialog1;
    private javax.swing.JMenuItem jMenuItem1;
    private javax.swing.JButton loginBtn;
    private javax.swing.JLabel loginPasswordLabel;
    private javax.swing.JLabel loginUsernameLabel;
    private javax.swing.JMenuBar mainMenuBar;
    private javax.swing.JPasswordField passwordTextField;
    private javax.swing.JLabel statusLabel;
    private javax.swing.JMenu toolsMenu;
    private javax.swing.JTextField userNameTextField;
    // End of variables declaration//GEN-END:variables
    
}
